﻿package nl.sanity.display 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import nl.sanity.debug.LogConsole;
	import nl.sanity.events.ResourceLoaderEvent;

	/**
	 * Resource
	 * Resource class with internal load functionality. Loads resources like an image or SWF.
	 * usage: var myImage:Resource = new Resource( "myImage.jpg", 120, 80 );
	 * 
	 * @langversion ActionScript 3
	 *
	 * @author M.P. Bartnig
	 * @since  28/03/2009
	 */
	public class Resource extends Sprite
	{
		public static var NAME		: String	= "Resource";
		public static var DEBUG		: Boolean	= false;
		
		protected var _url			: String;
		protected var _loader		: Loader;
		protected var _width		: Number;
		protected var _height		: Number;
		protected var _data			: Object;
		protected var _loadComplete	: Boolean;
		protected var _isSWF		: Boolean;
		protected var _isImage		: Boolean;
		protected var _smoothing	: Boolean;
		
		// --- CONSTRUCTOR ----------------------------------------

		public function Resource( url:String, width:Number = 0, height:Number = 0 )
		{
			if ( url )
			{
				if ( width > 0 ) _width = width;
				if ( height > 0 ) _height = height;
				
				_url	= url;
				_loader	= new Loader();
				_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete, false, 0, true );
				_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onIOError, false, 0, true );
				
				var request:URLRequest = new URLRequest( _url );
				_loader.load( request );
				
				LogConsole.info( NAME + " | Requesting URL '" + _url + "'" );
			}
			else
			{
				LogConsole.info( NAME + " | No URL specified" );
			}
		}
		
		// --- PUBLIC FUNCTIONS ----------------------------------------

		public function getBitmap( bitmapSmoothing:Boolean ):Bitmap
		{
			var clonedBitmapData:BitmapData = Bitmap( _loader.content ).bitmapData.clone();
			var bmp:Bitmap					= new Bitmap( clonedBitmapData );
			bmp.smoothing					= bitmapSmoothing || _smoothing;
			
			return bmp;
		}
		
		// --- PROTECTED FUNCTIONS ----------------------------------------

		protected function onComplete( event:Event ):void
		{
			LogConsole.info( NAME + ".onComplete | Resource loaded with contenttype '" + _loader.contentLoaderInfo.contentType + "'" );
			
			_loadComplete = true;
			
			if ( _loader.contentLoaderInfo.contentType == "application/x-shockwave-flash" )
			{
				if ( _width ) _loader.width = _width;
				if ( _height ) _loader.height = _height;
				
				_isSWF = true;
				
				addChildAt( _loader, 0 );
			}
			else
			{
				var image:Bitmap = Bitmap( _loader.content );
				
				if ( _width ) image.width = _width;
				if ( _height ) image.height = _height;
				
				image.smoothing = _smoothing;
				_isImage		= true;
				
				addChildAt( image, 0 );
			}
			
			dispatchEvent( new ResourceLoaderEvent( ResourceLoaderEvent.ON_COMPLETE, _data ) );
		}
		
		protected function onIOError( event:IOErrorEvent ):void 
		{
			LogConsole.info( NAME + ".onIOError | Couldn't load resource" );
			dispatchEvent( new ResourceLoaderEvent( ResourceLoaderEvent.ON_ERROR, _data ) );
		}
		
		// --- OVERRIDE FUNCTIONS ----------------------------------------

		override public function set width( value: Number ):void 
		{
			_width = value;
			
			if ( _loadComplete )
			{
				var content:DisplayObject	= getChildAt( 0 );
				content.width				= _width;
			}
		}
		
		override public function set height( value: Number ):void 
		{
			_height = value;
			
			if ( _loadComplete )
			{
				var content:DisplayObject	= getChildAt( 0 );
				content.height 				= _height;
			}
		}
		
		// --- GETTERS/SETTERS ----------------------------------------

		public function get url():String
		{
			return _url;
		}
		
		public function get loader():Loader
		{
			return _loader;
		}
	
		public function get loadComplete():Boolean
		{
			return _loadComplete;
		}
		
		
		public function get isSWF():Boolean
		{
			return _isSWF;
		}
		
		public function get isImage():Boolean
		{
			return _isImage;
		}
		
		public function get data():Object
		{
			return _data;
		}
		public function set data( value:Object ):void 
		{
			_data = value;
		}
		
		public function get smoothing():Boolean
		{
			return _smoothing;
		}
		public function set smoothing( value:Boolean ):void 
		{
			_smoothing = value;
		}
	}
}